package com.ruoyi.swla2.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.swla2.domain.PressureConfiguration;
import com.ruoyi.swla2.domain.WarnSs;
import com.ruoyi.swla2.mapper.PressureConfigurationMapper;
import com.ruoyi.swla2.mapper.TerminalHistoryDataMapper;
import com.ruoyi.swla2.mapper.WarnMapper;
import com.ruoyi.swla2.mapper.WarnSsMapper;
import com.ruoyi.swla2.model.MultiWarnDataModel;
import com.ruoyi.swla2.service.IPressureConfigurationService;
import com.ruoyi.swla2.service.IWarnSsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


@Service
public class WarnSsServiceImpl extends ServiceImpl<WarnSsMapper, WarnSs> implements IWarnSsService {

    @Autowired
    private WarnSsMapper warnSsMapper;


    @Override
    public List<MultiWarnDataModel> getMultiWarnlist() {
        List<MultiWarnDataModel> resList = new ArrayList<>();

        LambdaQueryWrapper<WarnSs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WarnSs::getWarnParamType,10);
        List<WarnSs> list = this.list(wrapper);

        if(!list.isEmpty()){
            List<MultiWarnDataModel> allList = warnSsMapper.getAllMultiConfig();
            Map<String,MultiWarnDataModel> idDataMap = allList.stream().collect(Collectors.toMap(MultiWarnDataModel::getId, Function.identity()));
            for (WarnSs warnSs : list) {
                String targetId = warnSs.getTargetID();
                if(idDataMap.containsKey(targetId)){
                    MultiWarnDataModel dataModel = idDataMap.get(targetId);
                    dataModel.setStartTime(DateUtil.formatDateTime(warnSs.getStartTime()));
                    dataModel.setEndTime(DateUtil.formatDateTime(warnSs.getRecentlyWartTime()));
                    dataModel.setValue(warnSs.getWarnValue());
                    resList.add(dataModel);
                }
            }
        }
        return resList;
    }

    @Override
    @InterceptorIgnore(tenantLine = "true")
    public List<WarnSs> selectListForTask(List<Long> configIds) {
        return warnSsMapper.selectList(new LambdaQueryWrapper<WarnSs>().in(WarnSs::getTargetID,configIds)
                .eq(WarnSs::getHandleStatus,0));
    }

    @Override
    @InterceptorIgnore(tenantLine = "true")
    public void updateForTask(WarnSs warn) {
        warnSsMapper.updateById(warn);
    }

    @Override
    @InterceptorIgnore(tenantLine = "true")
    public void insertForTask(WarnSs warn) {
        warnSsMapper.insert(warn);
    }
}
